package com.itzxx.huazhun.utils

import java.util.Calendar

object DateUtil {

    fun getYMDStr():String{
        val calendar = Calendar.getInstance()
        val currentYear = calendar.get(Calendar.YEAR)
        val currentMonth = calendar.get(Calendar.MONTH) + 1
        val currentDay = calendar.get(Calendar.DAY_OF_MONTH)
        return "${currentYear}-${String.format("%02d", currentMonth)}-${String.format("%02d", currentDay)}"
    }

    /**
     * 获取 yyyy-mm-dd hh:mm
     * @param timestamp Long
     * @return String
     */
    fun getYMDHMStr(timestamp:Long) :String{
        val calendar = Calendar.getInstance().apply {
            timeInMillis = timestamp
        }
        val currentYear = calendar.get(Calendar.YEAR)
        val currentMonth = calendar.get(Calendar.MONTH) + 1
        val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

        val hour = calendar.get(Calendar.HOUR_OF_DAY) // 获取小时（24小时制）
        val minute = calendar.get(Calendar.MINUTE) // 获取分钟
        val second = calendar.get(Calendar.SECOND) // 获取秒

        return "${currentYear}-${String.format("%02d", currentMonth)}-${String.format("%02d", currentDay)} ${String.format("%02d", hour)}:${String.format("%02d", minute)}"
    }

    /**
     * 获取 yyyy-mm-dd hh:mm:ss
     * @param timestamp Long
     * @return String
     */
    fun getYMDHMSStr(timestamp:Long) :String{
        val calendar = Calendar.getInstance().apply {
            timeInMillis = timestamp
        }
        val currentYear = calendar.get(Calendar.YEAR)
        val currentMonth = calendar.get(Calendar.MONTH) + 1
        val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

        val hour = calendar.get(Calendar.HOUR_OF_DAY) // 获取小时（24小时制）
        val minute = calendar.get(Calendar.MINUTE) // 获取分钟
        val second = calendar.get(Calendar.SECOND) // 获取秒

        return "${currentYear}-${String.format("%02d", currentMonth)}-${String.format("%02d", currentDay)} ${String.format("%02d", hour)}:${String.format("%02d", minute)}:${String.format("%02d", second)}"
    }

    /**
     * 获取 yyyy-mm-dd hh:mm:ss
     * @param timestamp Long
     * @return String
     */
    fun getYMDHMSStr(timestamp:String) :String{
        val calendar = Calendar.getInstance().apply {
            timeInMillis = timestamp.toLong()
        }
        val currentYear = calendar.get(Calendar.YEAR)
        val currentMonth = calendar.get(Calendar.MONTH) + 1
        val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

        val hour = calendar.get(Calendar.HOUR_OF_DAY) // 获取小时（24小时制）
        val minute = calendar.get(Calendar.MINUTE) // 获取分钟
        val second = calendar.get(Calendar.SECOND) // 获取秒

        return "${currentYear}-${String.format("%02d", currentMonth)}-${String.format("%02d", currentDay)} ${String.format("%02d", hour)}:${String.format("%02d", minute)}:${String.format("%02d", second)}"
    }

    fun formatHMSTime(timestamp:Long) :String{
        val calendar = Calendar.getInstance().apply {
            timeInMillis = timestamp
        }
        val hour = calendar.get(Calendar.HOUR_OF_DAY) // 获取小时（24小时制）
        val minute = calendar.get(Calendar.MINUTE) // 获取分钟
        val second = calendar.get(Calendar.SECOND) // 获取秒

        return "${String.format("%02d", hour)}:${String.format("%02d", minute)}:${String.format("%02d", second)}"
    }
}